using System;
using System.Collections.Generic;
using BanZuiCloudControlAgent.Core.Mqtt.Models;
using BanZuiCloudControlAgent.Core.Tasks.Base;
using BanZuiCloudControlAgent.Core.Tasks.Enums;
using BanZuiCloudControlAgent.Core.Tasks.Models;

namespace BanZuiCloudControlAgent.Core.Tasks.Utils
{
    /// <summary>
    /// 任务工具类，提供任务相关的辅助方法
    /// </summary>
    public static class TaskHelper
    {
        /// <summary>
        /// 创建任务上报负载
        /// </summary>
        /// <param name="task">任务</param>
        /// <returns>任务上报负载</returns>
        public static TaskReportPayloadModel CreateTaskReportPayload(BaseTask task)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }
            
            // 确保Data是Dictionary<string, object>类型
            Dictionary<string, object> data = null;
            if (task.Result?.Data != null)
            {
                // 如果已经是Dictionary<string, object>类型，直接使用
                if (task.Result.Data is Dictionary<string, object> dictData)
                {
                    data = dictData;
                }
                // 否则创建一个新的Dictionary并添加数据
                else
                {
                    data = new Dictionary<string, object> { { "data", task.Result.Data } };
                }
            }
            
            return new TaskReportPayloadModel
            {
                TaskId = task.TaskId,
                TaskType = task.TaskType.ToString(),
                TaskStatus = task.Status.ToString(),
                Progress = task.Progress,
                Result = task.Result?.Message,
                Data = data ?? new Dictionary<string, object>()
            };
        }
        
        /// <summary>
        /// 创建任务上报模型
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="deviceCode">设备码</param>
        /// <returns>任务上报模型</returns>
        public static TaskReportModel CreateTaskReport(BaseTask task, string deviceCode)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }
            
            var payload = CreateTaskReportPayload(task);
            return new TaskReportModel(deviceCode, payload);
        }
        
        /// <summary>
        /// 将任务类型字符串转换为枚举
        /// </summary>
        /// <param name="taskTypeString">任务类型字符串</param>
        /// <returns>任务类型枚举</returns>
        public static TaskType ConvertToTaskType(string taskTypeString)
        {
            if (string.IsNullOrEmpty(taskTypeString))
            {
                return TaskType.Unknown;
            }
            
            switch (taskTypeString.ToUpper())
            {
                case "ADD_GROUP":
                    return TaskType.AddGroup;
                case "SEND_MESSAGE":
                    return TaskType.SendMessage;
                case "INVITE":
                    return TaskType.InviteFriend;
                case "FORWARD_MESSAGE":
                    return TaskType.ForwardMessage;
                case "UPDATE":
                    return TaskType.Update;
                case "CONFIG":
                    return TaskType.Config;
                default:
                    return TaskType.Unknown;
            }
        }
    }
} 